
let netType=localStorage.getItem("netType");

let walletList = localStorage.getItem("wallet");
let index=localStorage.getItem("accountIndex");
let coinsList=localStorage.getItem("coinsList");


let switchList=localStorage.getItem("switchList");

let ethBalance=localStorage.getItem("ethBalance");
let ethPrice=localStorage.getItem("ethPrice");
let balance=localStorage.getItem("balance");

let messageList=localStorage.getItem("messageList");
let contactList=localStorage.getItem("contactList");




//有时候if(walletList)会失效
if(walletList!=null&&walletList!=''&&walletList!='undefined'&&walletList[0]!='undefined'){
  walletList = JSON.parse(walletList?walletList:[]);
}else{
  walletList=[]
}

if(messageList!=null&&messageList!=''&&messageList!='undefined'&&messageList[0]!='undefined'){
  messageList = JSON.parse(messageList?messageList:[]);
}else{
  messageList=[]
}

if(contactList!=null&&contactList!=''&&contactList!='undefined'&&contactList[0]!='undefined'){
  contactList = JSON.parse(contactList?contactList:[]);
}else{
  contactList=[]
}



if(!coinsList){
  coinsList=require("../service/eth/config").mainCoinList
  for(let i=0;i<coinsList.length;i++){
    let {abi,address}=require("../service/coin/"+coinsList[i].name);
    coinsList[i].abi=abi;
  }
  localStorage.setItem("coinsList",JSON.stringify(coinsList))
  console.log('存coinList')
}else{
  coinsList = JSON.parse(coinsList);
}


//保存数据的地方
const state={
  netType:netType?netType:'test',
  wallet:walletList,
  accountIndex:index?index:-1,
  coinsList:coinsList != null?coinsList:[],
  ethBalance:ethBalance!=null?ethBalance:0,
  ethPrice:ethPrice!=null?ethPrice:0,
  balance:balance!=null?balance:0,
  messageList:messageList!=null?messageList:[],
  contactList:contactList!=null?contactList:[],
  showNav:false
}

const getters={
  netType:state=>state.netType!= null&&state.netType.length>0,
  //判断是否已经创建钱包
  hasWallet:state=>state.wallet!= null&&state.wallet.length>0,
  //获得所有钱包列表
  wallet:state=>state.wallet,
  //获得当前选中账户
  currentAccount:state=>state.wallet[state.accountIndex],
  //获得选中账户下标
  accountIndex:state=>state.accountIndex,
  //获得代币开关列表
  coinsList:state=>state.coinsList,
  //以太坊余额
  ethBalance:state=>state.ethBalance,
  //以太坊价格
  ethPrice:state=>state.ethPrice,
  //余额
  balance:state=>state.balance,
  //消息通知
  messageList:state=>state.messageList,
  //联系人
  contactList:state=>state.contactList
}

//外部可以调用修改数据的方法,对外暴露的set
const actions={

  setNetType:(context,netType)=>{
    context.commit('mSetNetType',netType)
    localStorage.setItem("netType",JSON.stringify(state.netType))
  },
  setWallet:(context,wallet)=>{
    context.commit('mSetWallet',wallet)
    localStorage.setItem("wallet",JSON.stringify(state.wallet))
  },
  setAccountIndex:(context,accountIndex)=>{
    context.commit('mSetAccountIndex',accountIndex)
    localStorage.setItem("accountIndex",state.accountIndex)
  },
  setWalletAccountByIndex:(context,account,accountIndex)=>{
    context.commit('mSetWalletAccountByIndex',account,accountIndex)

  },
  setCoinByIndex:(context,coin,index)=>{
    context.commit('mSetCoinByIndex',coin,index)
    localStorage.setItem("coinsList",JSON.stringify(state.coinsList))
  },
  setCoinsList:(context,coinsList)=>{
    context.commit('mSetCoinsList',coinsList)
    localStorage.setItem("coinsList",JSON.stringify(state.coinsList))
  },
  setEthBalance:(context,ethBalance)=>{
    context.commit('mSetEthBalance',ethBalance)
    localStorage.setItem("ethBalance",JSON.stringify(state.ethBalance))
  },
  setEthPrice:(context,ethPrice)=>{
    context.commit('mSetEthPrice',ethPrice)
    localStorage.setItem("ethPrice",JSON.stringify(state.ethPrice))
  },
  setBalance:(context,balance)=>{
    context.commit('mSetBalance',balance)
    localStorage.setItem("balance",JSON.stringify(state.balance))
  },
  setMessageList:(context,messageList)=>{
    context.commit('mSetMessageList',messageList)
    localStorage.setItem("messageList",JSON.stringify(state.messageList))
  },
  setContactList:(context,contactList)=>{
    context.commit('mSetContactList',contactList)
    localStorage.setItem("contactList",JSON.stringify(state.contactList))
  }
}

//mutations 真正持久去修改数据的
const mutations={
  changeNav:(state,result)=>{
    state.showNav = result;
  },
  mSetNetType:(state,netType)=>{
    state.netType = netType;
  },
  mSetWallet:(state,wallet)=>{
    state.wallet = wallet;
  },
  mSetAccountIndex:(state,accountIndex)=>{
    state.accountIndex = accountIndex;
  },
  mSetWalletAccountByIndex:(state,account,accountIndex)=>{
    state.wallet[accountIndex] = account;
    localStorage.setItem("wallet",JSON.stringify(state.wallet))
  },
  mSetCoinByIndex:(state,coin,index)=>{
    state.coinsList[index] = coin;
  },
  mSetCoinsList:(state,coinsList)=>{
    state.coinsList = coinsList;
  },
  mSetEthBalance:(state,ethBalance)=>{
    state.ethBalance = ethBalance;
  },
  mSetEthPrice:(state,ethPrice)=>{
    state.ethPrice = ethPrice;
  },
  mSetBalance:(state,balance)=>{
    state.balance = balance;
  },
  mSetMessageList:(state,messageList)=>{
    state.messageList = messageList;
  },
  mSetContactList:(context,contactList)=>{
    state.contactList = contactList;
  }

}


export default {
  state,getters,actions,mutations
}
